package com.ego.user.controller;


import com.ego.exception.EgoException;
import com.ego.exception.ExceptionEnum;
import com.ego.user.pojo.UserAddress;
import com.ego.user.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@Slf4j
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @GetMapping("/list")
    public ResponseEntity<List<UserAddress>> getUserAddressList(){
        return ResponseEntity.ok(userAddressService.getUserAddressList());
    }

    @PostMapping("/add")
    public ResponseEntity<Void> add(@RequestBody UserAddress userAddress){
        int row = userAddressService.add(userAddress);
        if (row<=0){
            EgoException.error(log, ExceptionEnum.ADD_ADDRESS_EXCEPTION);
        }
        return ResponseEntity.ok().build();
    }

    /*/user/address/delete/"+id,*/
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id){
        int row = userAddressService.delete(id);
        if (row<=0){
            EgoException.error(log, ExceptionEnum.delete_ADDRESS_EXCEPTION);
        }
        return ResponseEntity.ok().build();
    }


}
